home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / ucrasm27.zip / SOURCE.ZIP / APPEND.ASM < prev    next >
Assembly Source File  |  1992-03-13  |  5KB  |  203 lines

  1.  
  2. ; Need to include "lists.a" in order to get list structure definition.
  3.  
  4.         include    lists.a
  5.         extrn    sl_malloc:far
  6.  
  7.  
  8. wp        equ    <word ptr>        ;I'm a lazy typist
  9.  
  10.  
  11. ; Special case to handle MASM 6.0 vs. all other assemblers:
  12. ; If not MASM 5.1 or MASM 6.0, set the version to 5.00:
  13.  
  14.         ifndef    @version
  15. @version    equ    500
  16.         endif
  17.  
  18.  
  19.  
  20. StdGrp        group    stdlib,stddata
  21. stddata        segment    para public 'sldata'
  22. stddata        ends
  23.  
  24. stdlib        segment    para public 'slcode'
  25.         assume    cs:stdgrp
  26.  
  27. ; sl_Append -    DX:SI points at a list node.
  28. ;        ES:DI points at a list.
  29. ;        CX contains a node number.
  30. ;        Insert the new node after the CXth node in the list
  31. ;
  32. ;
  33. ; Randall Hyde  3/13/92
  34.  
  35.  
  36.         public    sl_Append
  37. sl_Append    proc    far
  38.         push    ax
  39.         push    bx
  40.         push    ds
  41.         push    es
  42.         push    di
  43.  
  44.         if    @version ge 600
  45.  
  46. ; MASM 6.0 version goes here
  47.  
  48.         mov    ds, dx
  49.         xor    dx, dx
  50.         cmp    wp es:[di].List.Head+2, dx    ;Empty list?
  51.         jne    GetTheNode
  52.  
  53. ; At this point, the HEAD pointer is zero.  This only occurs if the
  54. ; list is empty.  So point the Head, CurrentNode, and Tail pointers to
  55. ; the new node.
  56. ;
  57. ; Note: Technically, the NIL pointer is 32-bits of zero. However, this
  58. ; package assumes that if the segment is zero, the whole thing is zero.
  59. ; So don't put any nodes into segment zero!
  60.  
  61.         mov    wp es:[di].List.Head, si
  62.         mov    wp es:[di].List.Head+2, dx
  63.         mov    wp es:[di].List.Tail, si
  64.         mov    wp es:[di].List.Tail+2, dx
  65.         mov    wp es:[di].List.CurrentNode, si
  66.         mov    wp es:[di].List.CurrentNode+2, dx
  67.  
  68.         mov    wp ds:[si].Node.Next, 0            ;Set all the links
  69.         mov    wp ds:[si].Node.Next+2, 0    ; in the first node
  70.         mov    wp ds:[si].Node.Prev, 0        ; to NIL.
  71.         mov    wp ds:[si].Node.Prev+2, 0
  72.         pop    di
  73.         pop    es
  74.         jmp    AppendDone
  75.  
  76.  
  77.  
  78. ; Okay, we have at least one node in the list, get the pointer to this node
  79. ; into ES:DI.
  80.  
  81. GetTheNode:    les    di, es:[di].List.Head        ;Get ptr to first node
  82.         jmp    short IntoLoop
  83.  
  84. ; The following loop repeats until we reach the end of the list or we count
  85. ; off CX nodes in the list.
  86.  
  87. FindNode:    les    di, es:[di].Node.Next
  88. IntoLoop:    cmp    dx, wp es:[di].Node.Next
  89.         loopne    FindNode
  90.  
  91.  
  92.         mov    ax, wp es:[di].Node.Next    ;Get Next ptr.
  93.         mov    bx, wp es:[di].Node.Next+2
  94.  
  95.  
  96.         mov    wp es:[di].Node.Next, si    ;Insert the new node
  97.         mov    wp es:[di].Node.Next+2, dx    ; after the current
  98.  
  99.         mov    wp ds:[si].Node.Prev, di    ;Link in ptr
  100.         mov    wp ds:[si].Node.Prev+2, es    ; to "current" node.
  101.  
  102.         mov    wp ds:[si].Node.Next, ax    ;Store away ptr to
  103.         mov    wp ds:[si].Node.Next+2, bx    ; previous node.
  104.  
  105.         mov    di, ax                ;Get ptr to NEXT
  106.         mov    es, bx                ; node.
  107.         mov    wp es:[di].Node.Prev, si    ;Store away link to
  108.         mov    wp es:[di].Node.Prev+2, dx    ; new node.
  109.  
  110.         pop    di                ;Retrieve pointer to
  111.         pop    es                ; list variable.
  112.         mov    wp es:[di].List.CurrentNode, si    ;Store ptr to new
  113.         mov    wp es:[di].List.CurrentNode+2, ds ; current node.
  114.  
  115.  
  116.  
  117.  
  118.         else
  119.  
  120. ; This code is for the other assemblers.
  121.  
  122.         mov    ds, dx
  123.         xor    dx, dx
  124.         cmp    wp es:[di].Head+2, dx    ;Empty list?
  125.         jne    GetTheNode
  126.  
  127. ; At this point, the HEAD pointer is zero.  This only occurs if the
  128. ; list is empty.  So point the Head, CurrentNode, and Tail pointers to
  129. ; the new node.
  130. ;
  131. ; Note: Technically, the NIL pointer is 32-bits of zero. However, this
  132. ; package assumes that if the segment is zero, the whole thing is zero.
  133. ; So don't put any nodes into segment zero!
  134.  
  135.         mov    wp es:[di].Head, si
  136.         mov    wp es:[di].Head+2, dx
  137.         mov    wp es:[di].Tail, si
  138.         mov    wp es:[di].Tail+2, dx
  139.         mov    wp es:[di].CurrentNode, si
  140.         mov    wp es:[di].CurrentNode+2, dx
  141.  
  142.         mov    wp ds:[si].Next, 0            ;Set all the links
  143.         mov    wp ds:[si].Next+2, 0    ; in the first node
  144.         mov    wp ds:[si].Prev, 0        ; to NIL.
  145.         mov    wp ds:[si].Prev+2, 0
  146.         pop    di
  147.         pop    es
  148.         jmp    AppendDone
  149.  
  150.  
  151.  
  152. ; Okay, we have at least one node in the list, get the pointer to this node
  153. ; into ES:DI.
  154.  
  155. GetTheNode:    les    di, es:[di].Head        ;Get ptr to first node
  156.         jmp    short IntoLoop
  157.  
  158. ; The following loop repeats until we reach the end of the list or we count
  159. ; off CX nodes in the list.
  160.  
  161. FindNode:    les    di, es:[di].Next
  162. IntoLoop:    cmp    dx, wp es:[di].Next
  163.         loopne    FindNode
  164.  
  165.  
  166.         mov    ax, wp es:[di].Next    ;Get Next ptr.
  167.         mov    bx, wp es:[di].Next+2
  168.  
  169.  
  170.         mov    wp es:[di].Next, si    ;Insert the new node
  171.         mov    wp es:[di].Next+2, dx    ; after the current
  172.  
  173.         mov    wp ds:[si].Prev, di    ;Link in ptr
  174.         mov    wp ds:[si].Prev+2, es    ; to "current" node.
  175.  
  176.         mov    wp ds:[si].Next, ax    ;Store away ptr to
  177.         mov    wp ds:[si].Next+2, bx    ; previous node.
  178.  
  179.         mov    di, ax                ;Get ptr to NEXT
  180.         mov    es, bx                ; node.
  181.         mov    wp es:[di].Prev, si    ;Store away link to
  182.         mov    wp es:[di].Prev+2, dx    ; new node.
  183.  
  184.         pop    di                ;Retrieve pointer to
  185.         pop    es                ; list variable.
  186.         mov    wp es:[di].CurrentNode, si    ;Store ptr to new
  187.         mov    wp es:[di].CurrentNode+2, ds ; current node.
  188.  
  189.  
  190.  
  191.  
  192.         endif
  193.  
  194. AppendDone:    pop    ds
  195.         pop    bx
  196.         pop    ax
  197.         ret
  198.  
  199. sl_Append    endp
  200.  
  201. stdlib        ends
  202.         end
  203.